package com.ea.eadp.nexus.test.pageDefinition.mobile;

import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import com.ea.eadp.nexus.test.pageDefinition.TwoFactorAuthClientWithQuestion;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class TFAClient_WebBasedMobile_Question extends TFAClient_WebBasedMobile implements TwoFactorAuthClientWithQuestion<WebDriver> {
    public static final By QUESTION = By.cssSelector(".SecurityQuestionText");
    public static final By ERROR_MESSAGE = By.cssSelector("form .submit_error_message");
    public static final By ANSWER = By.id("userAnswer");
    public static final By ANSWER_SUBMIT = By.cssSelector("#nxtBtn input[type='submit']");
    public static final By SUBMIT = By.cssSelector("#loginBtn input[type='submit']");
    public static final By ERROR_MESSAGE_LOGIN = By.cssSelector("#loginForm .general-error");


    @Override
    public void goAuthentication() throws Exception {
        super.getClient().get(TestUtil.BaseConnectURI() + TestUtil.SigninMobileConnectURI());
    }

    @Override
    public void goAuthenticationForSpecific(String clientId) throws Exception {
        super.getClient().get(TestUtil.BaseConnectURI() + TestUtil.getGenericConnectUri(clientId, "mobile/login"));
    }

    @Override
    public void goAuthenticationForSpecific(String clientId, String redirectUri) throws Exception {
        super.getClient().get(TestUtil.BaseConnectURI() + TestUtil.getGenericConnectUri(clientId, "mobile/login", redirectUri));
    }

    @Override
    public void login(String username, String password) throws Exception {
        super.getClient().findElement(USERNAME).clear();
        super.getClient().findElement(USERNAME).sendKeys(username);
        super.getClient().findElement(PASSWORD).clear();
        super.getClient().findElement(PASSWORD).sendKeys(password);

        super.getClient().findElement(SUBMIT).click();
    }

    @Override
    public void respondToChallenge(String answer) throws Exception {
        getClient().findElement(ANSWER).clear();
        getClient().findElement(ANSWER).sendKeys(answer);
        getClient().findElement(ANSWER_SUBMIT).click();
    }

    @Override
    public Status getStatus() throws Exception {
        Status result = super.getStatus();

        if (Status.UNKNOWN == result) {
            try {
                getClient().findElement(ANSWER);
                return Status.CHALLENGING_AUTH;
            } catch (NoSuchElementException e) {
                // do nothing
            }
        }

        return result;
    }

    @Override
    public String getQuestion() throws Exception {
        return getClient().findElement(QUESTION).getText().trim();
    }

    @Override
    public String getErrorMessage() throws Exception {
        try {
            return getClient().findElement(ERROR_MESSAGE).getText();
        } catch (NoSuchElementException e) {
        }
        try {
            return getClient().findElement(ERROR_MESSAGE_LOGIN).getText();
        } catch (NoSuchElementException e) {
        }
        return null;
    }
}
